// pages/history_data/history_data.js
// var wxCharts = require("../../../../utils/wxcharts.js");
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
var getTimes = i => {
  var last = new Date();
  const year = last.getFullYear()
  const day = last.getDate()
  const ti = day - i
  // 判断是否月初
  if (ti <= 0) {
    const month = last.getMonth() + 1 - 1
    const d = new Date(year, month, 0)
    const dayBig = d.getDate() //获取当月的所有天数
    const ti1 = dayBig + ti
    return [year, month, ti1].map(formatNumber).join('-')
  } else {
    const month = last.getMonth() + 1
    return [year, month, ti].map(formatNumber).join('-')
  }
}
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tablist:["血压","血糖","心率","体重"],
    tabIndex:0,
    array: ["近一周", '近两周', '近三周', '近一个月', '自定义'],
    index: 0,
    time_start: '2021-03-27',
    time_end: '2021-03-27',
  },
  // tab切换
  bindtab(e){
    this.setData({
      tabIndex: e.currentTarget.dataset.index,
      type: e.currentTarget.dataset.index,
    })
    this.getUser_pressure();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var object = {}
    var openid = options.openid;
    var j_id = options.j_id;
    that.setData({
      openid: openid,
      j_id: j_id
    })
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var time_end = Y + "-" + M + "-" + D;
    var time_start = getTimes(7)
    that.setData({
      time_start: time_start,
      time_end: time_end
    })
    that.getUser_pressure();
  },

  // 趋势折线图选择事件
  bindtype: function (e) {
    console.log(e);
    var that = this;
    var style = e.detail.value;
    that.setData({
      style: style,
      index: style
    })
    if (style != 4) {
      var timestamp = Date.parse(new Date());
      var date = new Date(timestamp);
      //获取年份  
      var Y = date.getFullYear();
      //获取月份  
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
      //获取当日日期 
      var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      var time_end = Y + "-" + M + "-" + D;
      if (style == 0) {
        var i = 6;
      } else if (style == 1) {
        var i = 13;
      } else if (style == 2) {
        var i = 20;
      } else if (style == 3) {
        var i = 30;
      }
      var time_start = getTimes(i)
      that.setData({
        time_start: time_start,
        time_end: time_end
      })
      that.getUser_pressure();
    }
  },
  // 开始时间选择
  bindDateStart: function (e) {
    console.log(e);
    var that = this;
    that.setData({
      time_start: e.detail.value,
      style: 4
    })
    that.getUser_pressure();
  },
  // 结束时间选择
  bindDateEnd: function () {
    console.log(e);
    var that = this;
    that.setData({
      time_start: e.detail.value,
      style: 4
    })
    that.getUser_pressure();
  },
  getUser_pressure: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/index.user_pressure",
      data: {
        openid: wx.getStorageSync('openid'),
        j_id: that.data.j_id,
        type: that.data.type,
        style: that.data.style,
        start: that.data.time_start,
        end: that.data.time_end
      },
      success: function (res) {
        that.setData({
          data: res.data
        })
        if (that.data.type == 0) {   //type=0血压折线图
          var date = res.data.date;
          var height = res.data.height;
          var low = res.data.low;
          var heart_rate = res.data.heart_rate
          try {
            var res = wx.getSystemInfoSync();
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight * 0.3;
          } catch (e) {
            console.error("系统信息错误")
          };
          new wxCharts({ //当月用电折线图配置
            canvasId: 'lineCanvas',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '心率',
              data: heart_rate,
              color: '#0087e0',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }, {
              name: '收缩压',
              data: height,
              color: '#ff7272',

              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            },
            {
              name: '舒张压',
              data: low,
              color: '#4dbb99',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 200,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
        }
        
      }
    })
  },
})